# Seymore - Content Management on Rails
# 
# Copyright (c) 2006 - 2007 Thomas Mango
# 
# For license information, please visit:
# http://code.google.com/p/seymore/
# 
# File created by Thomas Mango [tsmango@gmail.com]

class Revision < ActiveRecord::Base
  belongs_to :page
  belongs_to :user
  
  def revisions
    @revisions = Revision.find(:all, :conditions => ["page_id = ? ", page_id], :order => "created_at DESC")
  end
  
  def author
    if user != nil
      user
    else
      User.new :username => "system", :name => "System"
    end
  end

  def only?
    if Revision.count(:conditions => ["page_id = ?", page.id]) == 1
      return true
    else
      return false
    end
  end
  
  def self.find_most_recent(page)
    Revision.find(:first, :conditions => ["page_id = ?", page.id], :order => "created_at DESC")
  end

  def self.most_recent?(revision)
    if Revision.find_most_recent(revision.page).id == revision.id
      return true
    else
      return false
    end
  end
  
  def self.find_most_recent_published(page)
    Revision.find(:first, :conditions => ["page_id = ? AND published = ?", page.id, true], :order => "created_at DESC")
  end
  
  def self.most_recent_published?(revision)
    if Revision.find_most_recent_published(revision.page).id == revision.id
      return true
    else
      return false
    end
  end
end